#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#include "constants.h"
#include "basic_utils.hh"
#include "ransac.h"


ransac_params_t InitRansac()
{
    //lectura de las secuencias aleatorias para el ransac.
    char buffer[100];
    sprintf(buffer, "seqs%d.bin", PUNTOS_RANSAC);
    FILE * fseqbin = fopen(buffer, "r");
    if (!fseqbin)
    {
	printf("Error. El archivo de datos %s no existe o no se puede abrir.\n", buffer);
	exit(-1);
    }
    struct ransac_seq *rseq = (struct ransac_seq*)malloc(0);
    struct ransac_seq *prseq;
    int nseqs=0;
    int temp;
    fread(&temp, 1,sizeof(int), fseqbin);
    while (!feof(fseqbin))
    {
	if (temp != PUNTOS_RANSAC)
	{
	    printf("error en el archivo de datos %s\n", buffer);
	    exit(-1);
	}
	nseqs++;
	rseq = (struct ransac_seq*)realloc(rseq, sizeof(struct ransac_seq)*nseqs); 
	prseq = &rseq[nseqs-1];
	fread((void*)&prseq->head, sizeof(prseq->head), 1, fseqbin);
	prseq->seq_data = (unsigned char *)malloc(prseq->head.total_data*sizeof(unsigned char));
	fread(prseq->seq_data, prseq->head.total_data, sizeof(unsigned char), fseqbin);
	fread(&temp, 1,sizeof(int), fseqbin);
    }

    fclose(fseqbin);

   
    ransac_params_t mp;
    mp.pseqs = rseq;
    mp.nseqs = nseqs;
    mp.minpoints = rseq[0].head.m;
    mp.maxpoints = rseq[nseqs-1].head.m;

    return mp;
}
